package View;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextPane;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import Model.ListSlideModel;

import kdiapo.Main;
import kdiapo.SlideShow;
import kdiapo.CreationFrame;
import kdiapo.Resizable;

public class TextView extends ElementView{
	private String textValue;
	private JTextPane txt;
	private Resizable resizer;
	
	public TextView(CreationFrame _mainFrame,int _num, int _x, int _y, int _width, int _height, Color color, Color fontColor, Font font){
		
		super(_mainFrame,_num);
		    
	    this.txt = new JTextPane();
	    this.txt.setBounds(_x, _y, _width, _height);
	    this.txt.setBackground(color);
	    this.txt.setForeground(fontColor);
	    this.txt.setFont(font);
	    this.add(this.txt);
	    
	    this.resizer = new Resizable(this.txt,this);
	    
	    this.resizer.setBounds(_x, _y, _width, _height);
	    
	    txt.addCaretListener(new MyCaretListener());
	    
	    addListeners();
	}
	
	public TextView(CreationFrame _mainFrame, int _num, Color color, Color fontColor, Font font){
		super(_mainFrame,_num);
		  
		System.out.println("New Element : "+_num);	
	    
	    this.txt = new JTextPane();
	    
	    // Gotcha! Setting background has no effect with nimbus !
	    this.txt.setBackground(color);
	    
	    this.txt.setForeground(fontColor);
	    this.txt.setFont(font);
	    this.txt.setBounds(50, 50, 100, 50);
	    this.add(this.txt);
	    
	    this.resizer = new Resizable(this.txt,this);
	
	    this.resizer.setBounds(50, 50, 100,50);
	      
	    //Initialisation des valeurs de l'element texte
	    ListSlideModel md = this.getMainFrame().getModel();
	    md.setPosition(_num, 50, 50, 100, 50);
	    
	    addListeners();
	    
	    txt.addCaretListener(new MyCaretListener());
	    txt.addPropertyChangeListener("font", new FontListener());
	}
	
	public TextView(SlideShow _mainFrame,int _num, int _x, int _y, int _width, int _height, Color color, Color fontColor, Font font){
		super(_num);
		  
		
	    this.txt = new JTextPane();
	    this.txt.setBounds(_x, _y, _width, _height);
	    this.txt.setBackground(color);
	    this.txt.setForeground(fontColor);
	    this.txt.setFont(font);
	    this.add(this.txt);
	    
	    this.resizer = new Resizable(this.txt,this);
	    this.resizer.setBounds(_x, _y, _width, _height);
	    
	    //addListeners();
	}

	public void addListeners(){
		//mise � jour du mod�le a chaque lettre tap�e dans la textarea
		this.resizer.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {}			
			public void keyReleased(KeyEvent e) {
				
				if(e.getKeyCode() == KeyEvent.VK_DELETE){
					//Suppression de l'�l�ment
					removeElement(getNumElement());
				}
			}
			
			
			public void keyPressed(KeyEvent e) {}
		});
		
		
	    this.txt.addKeyListener(new KeyListener() {
	    	public void keyTyped(KeyEvent e) {}
			public void keyReleased(KeyEvent e) {
				//mise � jour du mod�le
				getMainFrame().getModel().setTextValue(getNumElement(),txt.getText());
				getMainFrame().printModel();
			
			}
			public void keyPressed(KeyEvent e) {}
		});
	    
	    this.resizer.addComponentListener(new ComponentListener() {
			public void componentShown(ComponentEvent e) {}
			public void componentResized(ComponentEvent e) {
				System.out.println(getNumElement()+"::largeur="+txt.getWidth()+"\tlongueur="+txt.getHeight());
				getMainFrame().getModel().setPosition(getNumElement(), 
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX(),
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getY(), 
						resizer.getWidth(),
						resizer.getHeight());
			}
			public void componentMoved(ComponentEvent e) {
				System.out.println(getNumElement()+"::X="+getMainFrame().getCurrentSlide().getComponent(0).getX()+"\tY="+getMainFrame().getCurrentSlide().getComponent(0).getX());
				getMainFrame().getModel().setPosition(getNumElement(), 
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getX(),
						getMainFrame().getCurrentSlide().getComponent(getNumElement()).getY(), 
						resizer.getWidth(),
						resizer.getHeight());
			}
			public void componentHidden(ComponentEvent e) {}
		});
	    this.resizer.addMouseListener(new MouseListener() {
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);	
			}
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger())
		            doPop(e);
			}
			public void mouseExited(MouseEvent e) {}
			public void mouseEntered(MouseEvent e) {}
			
			public void mouseClicked(MouseEvent e) {}
			public void doPop(MouseEvent e){
				JPopupMenu popMenu = new JPopupMenu();
				JMenuItem anItem =  new JMenuItem("Remove this element");
				anItem.setIcon(new ImageIcon(Main.class.getResource("/remove_icon.png")));
				anItem.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						//Suppression de l'�l�ment de la vue
						removeElement(getNumElement());
					}
				});
				
				popMenu.add(anItem);
				popMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
	public String getTextValue() {
		return textValue;
	}

	public void setTextValue(String textValue) {
		this.textValue = textValue;
		this.txt.setText(this.textValue);
	}

	public JTextPane getTxt() {
		return txt;
	}

	public void setTxt(JTextPane txt) {
		this.txt = txt;
	}

	public void removeElement(int num){
		getMainFrame().removeElement(num);
		//Mise � jour du mod�le
		getMainFrame().getModel().removeElement(num);
	}

	public Resizable getResizer() {
		return resizer;
	}

	public void setResizer(Resizable resizer) {
		this.resizer = resizer;
	}
	

	class MyCaretListener implements CaretListener{
		public void caretUpdate(CaretEvent arg0) {			
			updateSize();
		}
	}
	
	public void updateSize(){
		Dimension prefered = txt.getPreferredSize();
		int preferedWidth = (int) prefered.getWidth() + 20;
		int preferedHeight = (int) prefered.getHeight() + 20; 				
		if (preferedWidth <= 100)
			preferedWidth = 100;
		if (preferedHeight <= 50)
			preferedHeight = 50;
		resizer.setSize(preferedWidth, preferedHeight);	
	}
	
	class FontListener implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent arg0) {
			// TODO Auto-generated method stub
			updateSize();
		}
	}	
	
}
